﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualHint.SmartPropertyGrid;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;

namespace PropertyGridTest
{
    class MyPropertyGrid : VisualHint.SmartPropertyGrid.PropertyGrid
    {
        private int opacity;
        private bool _useWaitCursor;
        private Color _backColor;

        [PropertyValidator(typeof(PropertyValidatorMinMax), 0, 100)]
        public int Opacity
        {
            set { opacity = value; }
            get { return opacity; }
        }

        [PropertyValueDisplayedAs(new string[] { "Use", "Don't use" })]
        public new bool UseWaitCursor
        {
            set { _useWaitCursor = value; }
            get { return _useWaitCursor; }
        }

        
        public Color FormBackColor
        {
            get { return _backColor; }
            set { _backColor = value; }
        }

        protected override void OnPropertyChanged(VisualHint.SmartPropertyGrid.PropertyChangedEventArgs
e)
        {
            if (e.PropertyEnum.Property.Id == 3)
                Application.UseWaitCursor = UseWaitCursor;
            else if (e.PropertyEnum.Property.Id == 7)
                ParentForm.Opacity = (double)Opacity/100.0;
            else if (e.PropertyEnum.Property.Id == 5)
            {
                ParentForm.Opacity = FormBackColor.A / 255.0;
                Opacity = (int)(ParentForm.Opacity * 100.0);
                ParentForm.BackColor = Color.FromArgb(FormBackColor.R, FormBackColor.G,
                FormBackColor.B);
            }
            base.OnPropertyChanged(e);
        }


        protected override void OnInPlaceCtrlVisible(InPlaceCtrlVisibleEventArgs e)
        {
            if (e.PropertyEnum.Property.Id == 7)
                (e.InPlaceCtrl as PropInPlaceTrackbar).RealtimeChange = true;
            else if ((e.PropertyEnum.Parent.Property != null) &&
            (e.PropertyEnum.Parent.Property.Id == 6))
                (e.InPlaceCtrl as PropInPlaceUpDown).RealtimeChange = true;

            base.OnInPlaceCtrlVisible(e);
        }

        protected override void OnPropertyCreated(PropertyCreatedEventArgs e)
        {
            if (e.PropertyEnum.Parent.Property != null)
            {
                if (e.PropertyEnum.Parent.Property.Id == 6)
                    e.PropertyEnum.Property.Feel = GetRegisteredFeel(
                    VisualHint.SmartPropertyGrid.PropertyGrid.FeelUpDown);
            }
            base.OnPropertyCreated(e);
        }

        protected override void OnDisplayedValuesNeeded(DisplayedValuesNeededEventArgs e)
        {
            if (e.PropertyEnum.Property.Id == 8)
                e.DisplayedValues = new string[] {
                        "King", "Queen", "Rook", "Bishop", "Knight", "Pawn" };
            base.OnDisplayedValuesNeeded(e);
        }



        protected override void OnPropertyUpDown(PropertyUpDownEventArgs e)
        {
            if (e.PropertyEnum.Property.Id == 9)
                e.Value = (Double.Parse(e.Value) + (e.ButtonPressed ==
                PropertyUpDownEventArgs.UpDownButtons.Up ? 0.1 : -0.1)).ToString();
            base.OnPropertyUpDown(e);
        }

        public void Initialize()
        {
            PropertyEnumerator catEnum = AppendRootCategory(1, "Application");
            PropertyEnumerator propEum = AppendManagedProperty(catEnum, 2, "AllowQuit", typeof(bool), Application.AllowQuit, "", new ReadOnlyAttribute(true));
            AppendManagedProperty(catEnum, 2, "MyInteger", typeof(int), 4, "a test");
            catEnum = AppendRootCategory(4, "Form");
            AppendProperty(catEnum, 5, "Back color", ParentForm, "BackColor", "abc");
            propEum = AppendProperty(catEnum,6,"Size",ParentForm,"Size","");
            ExpandProperty(propEum, true);

            Opacity = (int)(ParentForm.Opacity * 100);
            propEum = InsertProperty(propEum, 7, "Opacity", this, "Opacity", "");
            propEum.Property.Feel = GetRegisteredFeel(
            VisualHint.SmartPropertyGrid.PropertyGrid.FeelTrackbarEdit);

            //propEum = InsertProperty(propEum, 7, "Opacity", ParentForm, "Opacity", "");
            propEum = AppendManagedProperty(catEnum, 10, "Size readonly", typeof(bool), false, "");
            propEum.Property.Feel = GetRegisteredFeel(
                VisualHint.SmartPropertyGrid.PropertyGrid.FeelCheckbox);
            propEum.Property.Look = new PropertyCheckboxLook();

            UseWaitCursor = Application.UseWaitCursor;
            AppendProperty(catEnum, 3, "Use wait cursor", this, "UseWaitCursor", "");


            propEum = AppendManagedProperty(catEnum, 8, "Chess piece", typeof(string), "Pawn", "");
            propEum.Property.Feel = GetRegisteredFeel(
            VisualHint.SmartPropertyGrid.PropertyGrid.FeelList);
            propEum = AppendManagedProperty(catEnum, 9, "Incremented value", typeof(double), 1.2, "");
            propEum.Property.Feel = GetRegisteredFeel(
            VisualHint.SmartPropertyGrid.PropertyGrid.FeelEditUpDown);

            propEum = AppendProperty(catEnum, 5, "Back color", this, "FormBackColor", "",
new PropertyDropDownContentAttribute(typeof(AlphaColorPicker)));
            propEum.Property.Feel = GetRegisteredFeel(
            VisualHint.SmartPropertyGrid.PropertyGrid.FeelList);
            propEum.Property.Look = new PropertyAlphaColorLook();
        }
    }
}
